#include <bits/stdc++.h>
using namespace std;
using uint=unsigned int;
using ll=long long;
using ull=unsigned long long;
using i128=__int128;
const int N=1e6+5;

int path[10],check[10];

void dfs(int n){
    if(n>8){
        for(int i=1;i<=8;i++){
            if(i>1)cout<<" ";
            cout<<path[i];
        }
        cout<<endl;
        return;
    }
    for(int i=1;i<=8;i++){
        if(!check[i]){
            path[n]=i;
            check[i]=1;
            dfs(n+1);
            check[i]=0;
        }
    }
    return;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    dfs(1);

    return 0;
}